<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区名" prop="communityId">
        <el-select v-model="queryParams.communityId" placeholder="请选择小区名" clearable>
          <el-option
            v-for="dict in dict.type.community"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋名" prop="buildId">
        <el-select v-model="queryParams.buildId" placeholder="请选择楼栋名" clearable>
          <el-option
            v-for="dict in dict.type.build"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务名" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="starTime">
        <el-date-picker clearable
                        v-model="queryParams.starTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="核酸地点" prop="nucleicSite">
        <el-input
          v-model="queryParams.nucleicSite"
          placeholder="请输入核酸地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!--      <el-form-item label="任务人数" prop="numberTasks">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.numberTasks"-->
      <!--          placeholder="请输入任务人数"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="完成人数" prop="completed">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.completed"-->
      <!--          placeholder="请输入完成人数"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['nucleicacid:buildtask:add']"
        >发布任务
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nucleicacid:buildtask:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nucleicacid:buildtask:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['nucleicacid:buildtask:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buildtaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="小区名" align="center" prop="communityId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.community" :value="scope.row.communityId"/>
        </template>
      </el-table-column>
      <el-table-column label="楼栋名" align="center" prop="buildId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.build" :value="scope.row.buildId"/>
        </template>
      </el-table-column>
      <el-table-column label="任务名" align="center" prop="taskName"/>
      <el-table-column label="状态" align="center" prop="status"/>
      <el-table-column label="开始时间" align="center" prop="starTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.starTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核酸地点" align="center" prop="nucleicSite"/>
      <el-table-column label="任务人数" align="center" prop="numberTasks"/>
      <el-table-column label="完成人数" align="center" prop="completed"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['nucleicacid:buildtask:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['nucleicacid:buildtask:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-comment"
            @click="handleSend(scope.row)"
            v-hasPermi="['nucleicacid:residenttask:post']"
          >短信提醒
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改楼栋核酸任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小区名" prop="communityId">
          <el-select v-model="form.communityId" placeholder="请选择小区名">
            <el-option
              v-for="dict in dict.type.community"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋名" prop="buildId">
          <el-select v-model="form.buildId" placeholder="请选择楼栋名">
            <el-option
              v-for="dict in dict.type.build"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务名" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="starTime">
          <el-date-picker clearable
                          v-model="form.starTime"
                          type="date"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="date"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="核酸地点" prop="nucleicSite">
          <el-input v-model="form.nucleicSite" placeholder="请输入核酸地点"/>
        </el-form-item>
        <!--        <el-form-item label="任务人数" prop="numberTasks">-->
        <!--          <el-input v-model="form.numberTasks" placeholder="请输入任务人数" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="完成人数" prop="completed">-->
        <!--          <el-input v-model="form.completed" placeholder="请输入完成人数" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBuildtask,
  getBuildtask,
  delBuildtask,
  addBuildtask,
  updateBuildtask,
  sendBuildtask
} from "@/api/nucleicacid/buildtask";

export default {
  name: "Buildtask",
  dicts: ['build', 'community'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼栋核酸任务表格数据
      buildtaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        communityId: null,
        buildId: null,
        taskName: null,
        status: null,
        starTime: null,
        endTime: null,
        nucleicSite: null,
        // numberTasks: null,
        // completed: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询楼栋核酸任务列表 */
    getList() {
      this.loading = true;
      listBuildtask(this.queryParams).then(response => {
        this.buildtaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        communityId: null,
        buildId: null,
        taskName: null,
        status: 0,
        starTime: null,
        endTime: null,
        nucleicSite: null,
        numberTasks: null,
        completed: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加楼栋核酸任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBuildtask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改楼栋核酸任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBuildtask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuildtask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除楼栋核酸任务编号为"' + ids + '"的数据项？').then(function () {
        return delBuildtask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 短信提醒 */
    handleSend(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认给小区"' + ids + '"发送核酸短信提醒？').then(function () {
        return sendBuildtask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("发送成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('nucleicacid/buildtask/export', {
        ...this.queryParams
      }, `buildtask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
